package com.bawei.bydayzg_two.payFragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.RatingBar
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.base.bean.OrderItem0
import com.bawei.base.mvvm.BaseFragment
import com.bawei.base.param.PComment
import com.bawei.bydayzg_two.R
import com.bawei.bydayzg_two.adapter.ImgAdapter
import com.bawei.bydayzg_two.adapter.OrderAdapter
import com.bawei.bydayzg_two.databinding.FragmentOrder1Binding
import com.bawei.bydayzg_two.databinding.FragmentOrder2Binding
import com.bawei.bydayzg_two.viewModel.OrderViewModel
import com.blankj.utilcode.util.ToastUtils
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

class OrderFragment2 : BaseFragment<FragmentOrder2Binding, OrderViewModel>() {
    lateinit var orderAdapter: OrderAdapter
    //
    var imgs = mutableListOf<String>()
    lateinit var imgAdapter: ImgAdapter
    //
    override fun getLoutyId(): Int {
        return R.layout.fragment_order2
    }

    override fun getViewModel(): Class<OrderViewModel> {
        return OrderViewModel::class.java
    }

    override fun initView() {

        //
        orderAdapter = OrderAdapter()
        binding.order1Rv.adapter = orderAdapter
        binding.order1Rv.layoutManager = LinearLayoutManager(context)
        //
        viewModel.order("1",20,1)

        //点击发货，弹出评论框
        orderAdapter.addChildClickViewIds(R.id.order_tv2)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.order_tv2->{
                    //弹出评论窗体
                    showPopupWindow(orderAdapter.data[position])
                }
            }
        }
    }

    private fun showPopupWindow(orderItem0: OrderItem0) {

        val popupWindow = PopupWindow()
        popupWindow.width = LayoutParams.MATCH_PARENT
        popupWindow.height = LayoutParams.WRAP_CONTENT
        //
        val inflate = LayoutInflater.from(context).inflate(R.layout.pop_msg, null)
        popupWindow.contentView = inflate//设置布局
        // 点击上传图片，可以再页面上展示
        inflate.findViewById<ImageView>(R.id.btn_up).setOnClickListener {
            PictureSelector.create(this,201).selectPicture()
        }
        //实例化适配器
        imgAdapter =  ImgAdapter()
        val rv = inflate.findViewById<RecyclerView>(R.id.rv_img)
        rv.adapter = imgAdapter
        rv.layoutManager = GridLayoutManager(requireActivity(),3)
        // 点击发送评论
        inflate.findViewById<Button>(R.id.btn_send).setOnClickListener {
            val pComment = PComment()
            pComment.comment = inflate.findViewById<EditText>(R.id.pop_et).text.toString()
            pComment.orderNo = orderItem0.orderId
            pComment.productId = orderItem0.orderInfoList[0].productId
            pComment.productScore = inflate.findViewById<RatingBar>(R.id.rb).rating.toInt()
            pComment.serviceScore = inflate.findViewById<RatingBar>(R.id.rb).rating.toInt()
            pComment.pics = imgs.toString()
            //
            viewModel.send(pComment)
            //
            popupWindow.dismiss()
        }
        //
        popupWindow.setOnDismissListener {
            imgs.clear()
        }
        //
        popupWindow.isOutsideTouchable = true
        popupWindow.isFocusable = true
        popupWindow.showAtLocation(binding.order1Rv,Gravity.BOTTOM,0,0)
    }

    override fun initDate() {

        //
        viewModel.orderSuccess.observe(this){
            if (it.code == 200){
                orderAdapter.data.clear()
                orderAdapter.data.addAll(it.data.list)
                orderAdapter.notifyDataSetChanged()
            }
            //
            ToastUtils.showLong(it.message)
        }

        //
        viewModel.sendSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }
    //
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //
        if (requestCode == 201 && resultCode == Activity.RESULT_OK){
            val pictureBean = data!!.extras!!.get(PictureSelector.PICTURE_RESULT) as PictureBean
            //
            imgs.addAll(listOf(pictureBean.path))
            //
            imgAdapter.data.clear()
            imgAdapter.data.addAll(imgs)
            imgAdapter.notifyDataSetChanged()
        }
    }

}